package com.kaibes.imserver.model.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.kaibes.imserver.model.utils.ByteStringUtil;

@SuppressWarnings("serial")
public class KaibesFile extends File{

	private FileOutputStream fileOutputStream;
	private String type;
	private int targetSize;
	
	public KaibesFile(String filepath, int targetSize) {
		super(filepath);
		init(targetSize);
	}

	public KaibesFile(File filefolder, String filename, int targetSize) {
		super(filefolder, filename);
		init(targetSize);
	}
	
	private void init(int targetSize) {
		this.targetSize = targetSize;
		
		if (!this.getParentFile().exists()) {
			this.getParentFile().mkdirs();
		}
		
		try {
			this.fileOutputStream = new FileOutputStream(this, true);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void write2File(String content){
		ByteStringUtil.string2FosBase64(content, getFileOutputStream());
	}
	
	public boolean isComplete(){
		if (this.length() < targetSize) {
			return false;
		}
		
		return true;
	}
	
	public void close(){
		try {
			if (fileOutputStream != null) {
				this.fileOutputStream.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public FileOutputStream getFileOutputStream() {
		return fileOutputStream;
	}

	public int getTargetSize() {
		return targetSize;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
